﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_Raovat_ThongSoCoBan
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            LoadType()
            If Not Request.QueryString("RaovatId") Is Nothing Then
                hidID.Value = Request.QueryString("RaovatId")
                ShowData()
            End If
        End If


    End Sub
    Protected Sub LoadType()
        Using Data As New VSEntities
           
            'Dong xe
            ddlDongXe.AppendDataBoundItems = True
            ddlDongXe.Items.Insert(0, New ListItem("---Chon---", "0"))
            Dim t = (From k In Data.vDongXes Select k).ToList
            ddlDongXe.DataTextField = "NAME"
            ddlDongXe.DataValueField = "ID"
            ddlDongXe.DataSource = t
            ddlDongXe.DataBind()

            'Tinhtrang
            ddlTinhTrang.AppendDataBoundItems = True
            ddlTinhTrang.Items.Insert(0, New ListItem("---Chon---", "0"))
            Dim e = (From v In Data.vTinhTrangs Select v).ToList
            ddlTinhTrang.DataTextField = "NAME"
            ddlTinhTrang.DataValueField = "ID"
            ddlTinhTrang.DataSource = e
            ddlTinhTrang.DataBind()

            'Mau ngoai that xe
            ddlMauNgoaiThat.AppendDataBoundItems = True
            ddlMauNgoaiThat.Items.Insert(0, New ListItem("---Chon---", "0"))
            Dim f = (From d In Data.vMauNgoaiThats Select d).ToList
            ddlMauNgoaiThat.DataTextField = "NAME"
            ddlMauNgoaiThat.DataValueField = "ID"
            ddlMauNgoaiThat.DataSource = f
            ddlMauNgoaiThat.DataBind()

            'Mau noi that xe
            ddlMauNoiThat.AppendDataBoundItems = True
            ddlMauNoiThat.Items.Insert(0, New ListItem("---Chon---", "0"))
            Dim u = (From s In Data.vMauNoiThats Select s).ToList
            ddlMauNoiThat.DataTextField = "NAME"
            ddlMauNoiThat.DataValueField = "ID"
            ddlMauNoiThat.DataSource = u
            ddlMauNoiThat.DataBind()

            'Nhien lieu
            ddlNhienLieu.AppendDataBoundItems = True
            ddlNhienLieu.Items.Insert(0, New ListItem("---Chon---", "0"))
            Dim a = (From b In Data.vNhienLieux Select b).ToList
            ddlNhienLieu.DataTextField = "NAME"
            ddlNhienLieu.DataValueField = "ID"
            ddlNhienLieu.DataSource = a
            ddlNhienLieu.DataBind()

            'He Thong Nhien lieu
            ddlHeThongNhienLieu.AppendDataBoundItems = True
            ddlHeThongNhienLieu.Items.Insert(0, New ListItem("---Chon---", "0"))
            Dim y = (From c In Data.vHeThongNhienLieux Select c).ToList
            ddlHeThongNhienLieu.DataTextField = "NAME"
            ddlHeThongNhienLieu.DataValueField = "ID"
            ddlHeThongNhienLieu.DataSource = y
            ddlHeThongNhienLieu.DataBind()

            'Hop so
            ddlHopSo.AppendDataBoundItems = True
            ddlHopSo.Items.Insert(0, New ListItem("---Chon---", "0"))
            Dim n = (From m In Data.vHopSoes Select m).ToList
            ddlHopSo.DataTextField = "NAME"
            ddlHopSo.DataValueField = "ID"
            ddlHopSo.DataSource = n
            ddlHopSo.DataBind()

            'Dan dong
            ddlDanDong.AppendDataBoundItems = True
            ddlDanDong.Items.Insert(0, New ListItem("---Chon---", "0"))
            Dim j = (From o In Data.vDanDongs Select o).ToList
            ddlDanDong.DataTextField = "NAME"
            ddlDanDong.DataValueField = "ID"
            ddlDanDong.DataSource = j
            ddlDanDong.DataBind()

        End Using
    End Sub

    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As ThongSoCoBan = (From q In data.ThongSoCoBans Where q.RaoVatId = hidRaoVatId.Value Select q).FirstOrDefault
            If Not p Is Nothing Then
                ' ddl = IIf(IsNothing(p.XuatXu) = True, "", p.XuatXu)
                ddlTinhTrang.SelectedValue = IIf(IsNothing(p.TinhTrang) = True, 0, p.TinhTrang)
                ddlDongXe.SelectedValue = IIf(IsNothing(p.DongXe) = True, 0, p.DongXe)
                txtSoKm.Text = IIf(IsNothing(p.SoKLMDaDi) = True, "", p.SoKLMDaDi)
                ddlMauNgoaiThat.SelectedValue = IIf(IsNothing(p.MauNgoaiThat) = True, 0, p.MauNgoaiThat)
                ddlMauNoiThat.SelectedValue = IIf(IsNothing(p.MauNoiThat) = True, 0, p.MauNoiThat)
                txtSocua.Text = IIf(IsNothing(p.SoCua) = True, "", p.SoCua)
                txtSochongoi.Text = IIf(IsNothing(p.SoChoNgoi) = True, "", p.SoChoNgoi)
                ddlNhienLieu.SelectedValue = IIf(IsNothing(p.NhienLieu) = True, 0, p.NhienLieu)
                ddlHeThongNhienLieu.SelectedValue = IIf(IsNothing(p.HeThongNhienLieu) = True, 0, p.HeThongNhienLieu)
                ddlHopSo.SelectedValue = IIf(IsNothing(p.HopSo) = True, 0, p.HopSo)
                ddlDanDong.SelectedValue = IIf(IsNothing(p.DanDong) = True, 0, p.DanDong)
                txtTieuthunhienlieu.Text = IIf(IsNothing(p.TieuThuNhienLieu) = True, 0, p.TieuThuNhienLieu)
                txtDescription.Text = IIf(IsNothing(p.Description) = True, 0, p.Description)
                hidRaoVatId.Value = IIf(IsNothing(p.RaoVatId) = True, 0, p.RaoVatId)
            End If
        End Using
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            If hidRaoVatId.Value <> "0" Then
                Dim p As ThongSoCoBan = (From q In data.ThongSoCoBans Where q.RaoVatId = hidRaoVatId.Value Select q).SingleOrDefault

                Try
                    ' p.XuatXu = txtXuatxu.Text.Trim()
                    p.TinhTrang = ddlTinhTrang.SelectedValue
                    p.DongXe = ddlDongXe.SelectedValue
                    p.SoKLMDaDi = txtSoKm.Text.Trim()
                    p.MauNgoaiThat = ddlMauNgoaiThat.SelectedValue
                    p.MauNoiThat = ddlMauNoiThat.SelectedValue
                    p.SoCua = txtSocua.Text.Trim()
                    p.SoChoNgoi = txtSochongoi.Text.Trim()
                    p.NhienLieu = ddlNhienLieu.SelectedValue
                    p.HeThongNhienLieu = ddlHeThongNhienLieu.SelectedValue
                    p.HopSo = ddlHopSo.SelectedValue
                    p.DanDong = ddlDanDong.SelectedValue
                    p.TieuThuNhienLieu = txtTieuthunhienlieu.Text.Trim()
                    p.Description = txtDescription.Text.Trim()
                    p.RaoVatId = hidRaoVatId.Value

                    data.SaveChanges()
                    ' Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Raovat/Step2.aspx?RaovatId=" + hidRaoVatId.Value + "';", Me.Page, True)
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
                End Try

            Else
                Dim p As New VSModel.ThongSoCoBan
                Try
                    ' p.XuatXu = txtXuatxu.Text.Trim()
                    p.TinhTrang = ddlTinhTrang.SelectedValue
                    p.DongXe = ddlDongXe.SelectedValue
                    p.SoKLMDaDi = txtSoKm.Text.Trim()
                    p.MauNgoaiThat = ddlMauNgoaiThat.SelectedValue
                    p.MauNoiThat = ddlMauNoiThat.SelectedValue
                    p.SoCua = txtSocua.Text.Trim()
                    p.SoChoNgoi = txtSochongoi.Text.Trim()
                    p.NhienLieu = ddlNhienLieu.SelectedValue
                    p.HeThongNhienLieu = ddlHeThongNhienLieu.SelectedValue
                    p.HopSo = ddlHopSo.SelectedValue
                    p.DanDong = ddlDanDong.SelectedValue
                    p.TieuThuNhienLieu = txtTieuthunhienlieu.Text.Trim()
                    p.Description = txtDescription.Text.Trim()
                    p.RaoVatId = hidRaoVatId.Value
                    data.ThongSoCoBans.AddObject(p)
                    data.SaveChanges()
                    ' Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Raovat/Step2.aspx?RaovatId=" + hidRaoVatId.Value + "';", Me.Page, True)
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
                End Try
            End If
           
        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
